package com.linck.im.client.handler;

import com.linck.im.client.context.AccountContext;
import com.linck.im.client.context.StageContext;
import com.linck.im.client.controller.LoginController;
import com.linck.im.model.proto.ProtoMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 系统消息处理器
 *
 * @author: linck
 * @create: 2021-01-25
 */
public class ClientSysMsgHandler extends SimpleChannelInboundHandler<ProtoMsg.SysMsg> {

    private static final Logger log = LoggerFactory.getLogger(ClientSysMsgHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtoMsg.SysMsg sysMsg) throws Exception {
        log.info("系统信息：code:{} msg:{}", sysMsg.getCode(), sysMsg.getMsg());
        // 登录事件，登录计数器+1
        if (sysMsg.getCode().equals(ProtoMsg.MsgCode.LOGIN_SUCCESS)) {
            AccountContext.LOGIN_STATUS = 1;
            LoginController.drawWelcomeText("登录成功");
            StageContext.changeChatView();
        } else if (sysMsg.getCode().equals(ProtoMsg.MsgCode.LOGIN_FAILURE)) {
            AccountContext.LOGIN_STATUS = -1;
            LoginController.drawWelcomeText("登录失败:" + sysMsg.getMsg());
        }
    }

    // 连接断开，设置未未登录
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        AccountContext.LOGIN_STATUS = 0;
    }
}
